//
//  RGLoginResetPasswordViewController.m
//  RedGrass
//
//  Created by wukong on 2019/1/2.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGLoginResetPasswordViewController.h"
#import "RGLoginResetPasswordView.h"
#import "RGLoginResetPassViewModel.h"

@interface RGLoginResetPasswordViewController ()<RGLoginResetPasswordViewDelegate>

@property (strong, nonatomic) RGLoginResetPasswordView *passwordResetView;
@property (strong, nonatomic) RGLoginResetPassViewModel *viewModel;
@property (assign, nonatomic) BOOL isFristLoad;

@end

@implementation RGLoginResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.NavStyleType = RGCustomNavStyleType_default;
    self.customNavBar.title = @"找回密码";
    _isFristLoad = YES;
    [self showBackButtonWithImage:@"返回_登录" target:self selector:@selector(backTo)];
    [self.customContentView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(self.view.mas_safeAreaLayoutGuideBottom).mas_offset(34);
        }else{
            make.bottom.mas_equalTo(0);
        }
    }];
    [self.customContentView addSubview:self.passwordResetView];
    [_passwordResetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsZero);
    }];
}

-(void)backTo{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if(_isFristLoad){
        [self.viewModel gainCaptchaCodeWithPhone:_phone];
        _isFristLoad = NO;
    }
}

#pragma mark - viewDelegate
-(void) gainCaptchaCodeWithView:(RGLoginResetPasswordView *)view{
    
    [self.viewModel gainCaptchaCodeWithPhone:_phone];
}

-(void) resetPassWithView:(RGLoginResetPasswordView *)view code:(NSString *)code pass:(NSString *)pass{
    if(code.length == 0){
        [self showTextMessageWithMessage:@"请输入验证码"];
        return;
    }
    if(pass.length == 0){
        [self showTextMessageWithMessage:@"密码不能为空"];
        return;
    }
    [self.viewModel resetPasswordWithPhone:_phone captcha:code newPass:pass];
}

#pragma mark - protocol
-(void) loginWithPasswordSuccess{
    [[AppDelegate shareAppDelegate] reInitTabarViewController];
    KPostNotification(KNotificationLoginStateChange, @YES);
}

//开始倒计时
-(void) ViewBegainGainCodeTimer{
    [_passwordResetView begainGainCodeTimer];
}

#pragma mark - getter

-(RGLoginResetPasswordView *)passwordResetView{
    if(!_passwordResetView){
        _passwordResetView = [[RGLoginResetPasswordView alloc]init];
        _passwordResetView.viewDelegate = self;
    }
    return _passwordResetView;
}

-(RGLoginResetPassViewModel *)viewModel{
    if(!_viewModel){
        _viewModel = [[RGLoginResetPassViewModel alloc]initWithProtocol:self];
    }
    return _viewModel;
}

@end
